package com.tms.app.tms.ui.home.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.bumptech.glide.Glide;
import com.jph.takephoto.model.TResult;
import com.tms.app.tms.R;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.UserInfo;
import com.tms.app.tms.bean.response.UserInfoRes;
import com.tms.app.tms.ui.InfoCarActivity;
import com.tms.app.tms.ui.InfoMeActivity;
import com.tms.app.tms.ui.base.ui.BasePhotoFragment;
import com.tms.app.tms.ui.setting.SettingHomeActivity;
import com.tms.app.tms.utils.PhotoHelper;
import com.tms.app.tms.utils.SharePreferenceUtil;
import com.tms.app.tms.widget.MyDialogPhoto;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.http.RequestParams;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by happy on 2017/8/26.
 */

public class AboutMeFt extends BasePhotoFragment implements View.OnClickListener {
    Unbinder unbinder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    UserInfoPresenter uPresenter;
    @BindView(R.id.img_head)
    CircleImageView imgHead;
    @BindView(R.id.text_login_name)
    TextView textLoginName;
    @BindView(R.id.text_login_info_)
    TextView textLoginInfo;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.text_me_status)
    TextView textMeStatus;
    @BindView(R.id.text_car_status)
    TextView textCarStatus;
    @BindView(R.id.text_guache_status)
    TextView textGuacheStatus;
    private UserInfo userInfo;
    private int ABOUT_ME = 0x200;
    private MyDialogPhoto myDialogPhoto;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_fragment_about, null);
        unbinder = ButterKnife.bind(this, rootView);
        uPresenter = new UserInfoPresenter();
        uPresenter.setiView(this);
        swipeLayout.setOnRefreshListener(uPresenter);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        uPresenter.getUserInfo(SharePreferenceUtil.getString(getContext(), "token", ""));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //  uPresenter.getUserInfo(SharePreferenceUtil.getString(getContext(), "token", ""));
        }
    }

    @OnClick({R.id.text_settings, R.id.parent_me, R.id.parent_car, R.id.parent_guache, R.id.img_head})
    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.text_settings:
                intent = new Intent(getContext(), SettingHomeActivity.class);
                startActivityForResult(intent, ABOUT_ME);
                break;
            case R.id.dialog_photo_text_top_1:
                PhotoHelper.takePhoto(getTakePhoto(), false);
                if (myDialogPhoto != null)
                    myDialogPhoto.close();
                break;
            case R.id.dialog_photo_text_top_2:
                PhotoHelper.pickPhoto(getTakePhoto(), false);
                if (myDialogPhoto != null)
                    myDialogPhoto.close();
                break;
            case R.id.img_head:
                myDialogPhoto = new MyDialogPhoto(getContext(), this, 1);
                myDialogPhoto.show();
                break;
            case R.id.parent_me:
                intent = new Intent(getContext(), InfoMeActivity.class);
                startActivityForResult(intent, ABOUT_ME);
                break;
            case R.id.parent_car:
                intent = new Intent(getContext(), InfoCarActivity.class);
                intent.putExtra("requestType", 0);
                startActivityForResult(intent, ABOUT_ME);
                break;
            case R.id.parent_guache:
                intent = new Intent(getContext(), InfoCarActivity.class);
                intent.putExtra("requestType", 1);
                startActivityForResult(intent, ABOUT_ME);
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void subscribe(UserInfoRes userInfoRes) {
        if (userInfoRes.getCode() == 200) {
            if (userInfoRes.getData() != null && userInfoRes.getData() != null)
                bindDataToView(userInfoRes.getData());
        } else {
            showMsg(userInfoRes.getError());
        }
    }

    private void bindDataToView(UserInfo userInfo) {
        this.userInfo = userInfo;
        if (userInfo == null|| StringUtil.isEmpty(userInfo.getMobile()))
            return;
        if (userInfo.getMobile() != null) {
            textLoginInfo.setText(userInfo.getMobile());
        }
        if (userInfo.getDriverName() != null) {
            textLoginName.setText(userInfo.getDriverName());
        }
        if (userInfo.getPhoto() != null) {

            Glide.with(getContext()).load(userInfo.getPhoto()).error(R.mipmap.about_me).into(imgHead);
        } else if (userInfo.getDriverImgPath() != null) {
            Glide.with(getContext()).load(Constant.IMG_URL + userInfo.getDriverImgPath()).error(R.mipmap.about_me).into(imgHead);
        }
         if (userInfo.getAuth_status()==1)
            textMeStatus.setText("已认证");
        if (userInfo.getHead_auth_status()==1)
            textCarStatus.setText("已认证");
        if (userInfo.getTrailer_auth_status()==1)
            textGuacheStatus.setText("已认证");
        SharePreferenceUtil.saveObject(getContext(), userInfo);

    }
    private int CAR_RESULT_OK=0x323;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0x212) {
            getActivity().finish();
        }
        if (resultCode==CAR_RESULT_OK){
            uPresenter.onRefresh();
        }
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        Glide.with(getContext()).load(result.getImage().getCompressPath()).error(R.mipmap.about_me).into(imgHead);
        RequestParams params = new RequestParams(Constant.USER_HEad);
        params.addBodyParameter("token", token);
        params.setMultipart(true);
        params.addBodyParameter("file", new File(result.getImage().getCompressPath()));
        uPresenter.getObjectFromServer(params, 1);
    }
}
